﻿@model IEnumerable<DAMS.Models.Article>

@{
    ViewData["Title"] = "List";
}

<h1>List</h1>

<p>
    <a asp-action="Create">Create New</a>
</p>
<table class="table table-hover">
    <thead class="thead-dark">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.ID)
            </th>
            <th width="38%">
                @Html.DisplayNameFor(model => model.Title)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Auth)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.PublishTime)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.isTop)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.isPublish)
            </th>

            <th>
                @Html.DisplayNameFor(model => model.CarouselPath)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.CoverPath)
            </th>


            <th width="15%">
                @Html.DisplayNameFor(model => model.CategoryID)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.ClickNum)
            </th>


            <th>操作</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.ID)
                </td>
                <td>
                    <a asp-action="Article" asp-controller="Home" asp-route-id="@item.ID"> @item.Title</a>

                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Auth)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.PublishTime)
                </td>
                <td>
                    @(Html.Raw(item.isTop ? "<img src=\"/css/media/images/icon_yes.png\">" : ""))
                </td>
                <td>
                    @(Html.Raw(item.isPublish ? "<img src=\"/css/media/images/icon_yes.png\">" : ""))
                </td>

                <td>
                    @if (item.isCarousel)
                    {
                        <img src="~/css/media/images/icon_picture.png" data-toggle="tooltip" data-html="true" dataurl="@item.CarouselPath" style="width:20px;" />
                    }

                </td>
                <td>
                    <img src="~/css/media/images/icon_picture.png" data-toggle="tooltip" data-html="true" dataurl="@item.CoverPath" style="width:20px;" />

                </td>

                <td>
                    <span dataCategoryID="@item.CategoryID"></span>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.ClickNum)
                </td>


                <td>
                    @Html.ActionLink("编辑", "Edit", new { id = item.ID }) |
                    @Html.ActionLink("查看", "Details", new { id = item.ID }) |
                    @Html.ActionLink("删除", "Delete", new { id = item.ID })
                </td>
            </tr>
        }
    </tbody>
</table>
@section Scripts {
    <script>
        var CategoryTreeJson = function (json) {
            var nodes = json;
            getNodeByID = function (cid) {
                for (var i in nodes) {
                    if (nodes[i].id == cid)
                        return nodes[i];
                }
            };
            getParentNameList = function (pid) {
                var paths = new Array();
                while (pid != 0) {
                    var node = getNodeByID(pid);
                    pid = node.ParentID;
                    paths.unshift(node.name);
                }
                return paths;
            };
            this.getNameList = function (cid) {
                var currNode = getNodeByID(cid);
                var nameList = getParentNameList(currNode.ParentID);
                return nameList.concat(currNode.name);
            }
        }
        $(function () {
            //鼠标移至封面和
            var treeJson = @Html.Raw(ViewData["TreeJson"]);
            var ctree = new CategoryTreeJson(treeJson);
            
            $("span[dataCategoryID]").each(function () {
                var cid = $(this).attr("dataCategoryID");
                $(this).html(ctree.getNameList(cid).join("<span style='color:red;font-weight:bold;'> › </span>"));
                
            })
            console.log();
            $('[data-toggle="tooltip"]').tooltip({
                template: '<div class="tooltip" role="tooltip"><div class="tooltip-inner" style="max-width:260px;"></div></div>',
                title: function () {
                    return '<span>' + $(this).attr('dataurl') + '</span><img src="' + $(this).attr('dataurl') + '" style="width:240px;" />';
                }
            })
        })
        
    </script>
}
